package com.peng.test.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.peng.utils.JacksonUtil;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * SampleFastJson1
 *
 * @author: lupeng6
 * @create: 2023/4/11 18:17
 */
public class SampleFastJson1 {

    @Test
    public void t1() {
        String json = """
                {
                  "1": {
                    "arr": [
                      {
                        "name": "zhangsan",
                        "age": 111
                      },
                      {
                        "name": "lisi",
                        "age": 222
                      }
                    ]
                  }
                }
                """;
        Map<String, Map<String, Object>> actionContent = JSON.parseObject(json, new TypeReference<Map<String, Map<String, Object>>>() {
        });
        Map<String, Object> map = actionContent.get("1");
        Object arr = map.get("arr");
        System.out.println(arr);
    }

    @Test
    public void t2() {
        String json = "[{\"name\":\"zhangsan\",\"age\":111},{\"name\":\"lisi\",\"age\":222}]";
        List<P1> p1s = JSON.parseArray(json, P1.class);
        System.out.println(p1s);
    }

    @Test
    public void t3() {
        String json = "[{\"name\":\"zhangsan\",\"age\":111},{\"name\":\"lisi\",\"age\":222}]";
        Collection<P1> p1s1 = JacksonUtil.ofJsonCollection(json, ArrayList.class, P1.class);
        System.out.println(p1s1);
    }


    @Data
    static class P1 {
        private String name;
        private int age;
    }
}
